﻿Public Class FormMain
    Private objProdInfo As New clsProdInfo
    Private Sub FormMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        connectDB()
        If String.IsNullOrEmpty(txtOper.Text) Then
            txtOper.Enabled = True
            btnChangeOper.Text = "保存"
        Else
            txtOper.Enabled = False
            btnChangeOper.Text = "切换用户"
        End If
        setData()
        txtBarCode.Select()
    End Sub
    Private Sub connectDB()
        g_szInitFileName = My.Application.Info.DirectoryPath & "\tm.ini"

        g_Server.szDrive = GetIniFile("SERVER", "DRIVE", "PostgreSQL ANSI", g_szInitFileName)
        g_Server.szIPadr = GetIniFile("SERVER", "IP", "", g_szInitFileName)
        g_Server.szDbn = GetIniFile("SERVER", "DBN", "ZHENANSF", g_szInitFileName)
        g_Server.szUid = GetIniFile("SERVER", "UID", "sa", g_szInitFileName)
        g_Server.szPas = GetIniFile("SERVER", "PAS", "sa", g_szInitFileName)
        g_Server.iOver = CInt(GetIniFile("SERVER", "OVER", "-1", g_szInitFileName))
        txtOper.Text = GetIniFile("LOGIN", "user", "", g_szInitFileName)

        If False = g_fDBconnect And 0 = g_iDBconnResult Then
            If "" = g_Server.szIPadr Or "0.0.0.0" = g_Server.szIPadr Then
            Else
                g_fDBconnect = True

                'データベース接続
                With g_Server
                    g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    If 1 <> g_iDBconnResult Then
                        .szDrive = CStr(IIf("PostgreSQL" = .szDrive, "PostgreSQL ANSI", "PostgreSQL"))
                        g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    End If
                End With
            End If
        End If

    End Sub

    Private Sub btnChangeOper_Click(sender As Object, e As EventArgs) Handles btnChangeOper.Click
        If btnChangeOper.Text.Equals("切换用户") Then
            txtOper.Enabled = True
            btnChangeOper.Text = "保存"
        Else
            If btnChangeOper.Text.Equals("保存") Then
                If String.IsNullOrEmpty(txtOper.Text.Trim()) = False Then
                    PutIniFile("LOGIN", "user", txtOper.Text.Trim(), g_szInitFileName)
                    txtOper.Enabled = False
                    btnChangeOper.Text = "切换用户"
                    txtBarCode.Focus()
                Else
                    MsgBox("请输入操作员账号")
                End If
            End If
        End If

    End Sub

    Private Sub lblQuit_Click(sender As Object, e As EventArgs) Handles lblQuit.Click
        PutIniFile("LOGIN", "user", "", g_szInitFileName)
        txtOper.Text = GetIniFile("LOGIN", "user", "", g_szInitFileName)
    End Sub

    Private Sub setData()
        Dim dt As DataTable = objProdInfo.getProdInfoByStatus("7")
        dgv.AutoGenerateColumns = False
        dgv.DataSource = dt
    End Sub


    Private Sub txtBarCode_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtBarCode.KeyPress
        If e.KeyChar = vbCr Then
            If String.IsNullOrEmpty(txtBarCode.Text.Trim()) = False Then
                Dim dt As DataTable = objProdInfo.getProdInfoByProdId(txtBarCode.Text.Trim())
                If dt.Rows.Count = 1 Then
                    Dim dr As DataRow = dt.Rows(0)
                    txt_prod_spec.Text = dr("prod_spec_id")
                    SendKeys.Send("{Tab}")
                Else
                    If dt.Rows.Count = 0 Then
                        MsgBox("此条码信息不存在")
                    Else
                        MsgBox("此条码信息存在多条记录")
                    End If
                End If
            Else
                MsgBox("请扫描或输入条码值")
            End If

        End If
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        If String.IsNullOrEmpty(txtOper.Text.Trim()) = False Then
            If String.IsNullOrEmpty(txtBarCode.Text.Trim()) = False Then
                Dim dtx As DataTable = objProdInfo.getProdInfoByProdIdAndStatus(txtBarCode.Text.Trim(), "6")
                If dtx.Rows.Count = 1 Then
                    If objProdInfo.setStatus(txtBarCode.Text.Trim(), txtOper.Text.Trim()) <> -2 Then
                        setData()
                        txtBarCode.Text = String.Empty
                        txt_prod_spec.Text = String.Empty
                        txtBarCode.Focus()
                        MsgBox("保存成功")
                    Else
                        MsgBox("保存失败")
                    End If
                Else
                    MsgBox("未查询到硫化完状态下的此生产编号信息")
                End If
            Else
                MsgBox("请扫描或输入条码值")
            End If
        Else
            MsgBox("请切换用户，以获取用户")
        End If
    End Sub

    Private Sub FormMain_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        DBacs.Disconnect()
    End Sub

End Class
